package com.comtime.smartbox;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.comtime.entity.ConsultBaseAdapter;
import com.comtime.entity.NewListInfo;
import com.comtime.info.InfoActivity;
import com.comtime.smartkilimanjaro.R;
import com.comtime.utils.LogUtils;
import com.comtime.utils.MyConfig;
import com.comtime.utils.Util;
import com.comtime.view.MyDialog;
import com.comtime.view.XListView;
import com.comtime.view.XListView.IXListViewListener;
import com.comyou.comyouhttp.ComyouHttpCallBack;
import com.comyou.comyouhttp.ComyouHttpClient;
import com.comyou.comyouhttp.ComyouHttpProgram;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

public class Activity_1 extends BaseActivity implements IXListViewListener{

	XListView xListView;
	ConsultBaseAdapter adapter;
	public static String INFOLISTS="newinfo";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_1);
		init();
		getInfo();
	}
	
	void init(){
		xListView=(XListView) findViewById(R.id.listview);
		xListView.setDivider(null);
		adapter = new ConsultBaseAdapter(this, xListView, newInfos);
		xListView.setAdapter(adapter);
		
		xListView.setPullLoadEnable(true);
		xListView.setPullRefreshEnable(true);
		xListView.setXListViewListener(this);
		xListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Log.i("tag", "position:"+position);

				Intent intent=new Intent(Activity_1.this, InfoActivity.class);
				intent.putExtra(INFOLISTS, newListInfos.get(position-1));
				startActivity(intent);
				
			}
		});
	}

	
	void showCancelDialog(String string){
		final MyDialog myDialog=new MyDialog(Activity_1.this);
		myDialog.setContent(string);
		myDialog.getLeftButton().setText(getText(R.string.ok));
		myDialog.getLeftButton().setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				myDialog.dismiss();
			}
		});
		myDialog.show();
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		if (Util.hasNetwork(Activity_1.this)) {
			onLoad();
			getInfo();
		} else {
			handler.postDelayed(new Runnable() {
				@Override
				public void run() {

					onLoad();
					showToast(getResources().getString(R.string.no_network)
							.toString());
				}
			}, 2000);
		}
	}

	Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				if(newInfos!=null){
					newInfos.clear();
					adapter.notifyDataSetChanged();
				}
				if(newListInfos!=null){
					newInfos.addAll(newListInfos);
					adapter.notifyDataSetChanged();
				}
				
				break;

			default:
				break;
			}
		};
	};
	@Override
	public void onLoadMore() {
		if (Util.hasNetwork(Activity_1.this)) {
			onLoad();
			getInfo();
		} else {
			handler.postDelayed(new Runnable() {
				@Override
				public void run() {

					onLoad();
					showToast(getResources().getString(R.string.no_network)
							.toString());
				}
			}, 2000);
		}
		
	}
	private void onLoad() {
		xListView.stopRefresh();
		xListView.stopLoadMore();
		xListView.onRefreshComplete();
	}
	
	
	
	
	
	/**
	 * 自定义提示
	 * 
	 * @param str
	 */
	Toast toast = null;

	void showToast(String str) {
		if (toast != null) {
			toast.cancel();
		}
		LayoutInflater inflater = getLayoutInflater();
		View view = inflater.inflate(R.layout.toastshow, null);
		TextView textView = (TextView) view.findViewById(R.id.toastshow);
		textView.setText(str);
		toast = new Toast(this);
		double d = getResources().getDisplayMetrics().density;
		toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.CENTER, 0, 0);
		toast.setDuration(1500);
		toast.setView(view);
		toast.show();
	}
	
	ArrayList<NewListInfo> newInfos=new ArrayList<NewListInfo>();
	ArrayList<NewListInfo> newListInfos=new ArrayList<NewListInfo>();
	
	void getInfo(){
//		final MyProgressDialog progressDialog = new MyProgressDialog(
//				Activity_1.this);
//		progressDialog.setContent(getText(R.string.loading).toString());
//		progressDialog.show();
		ComyouHttpProgram httpProgram = new ComyouHttpProgram();
		ComyouHttpClient httpClient = new ComyouHttpClient(MyConfig.IP
				+ "Home/newsList");
		httpClient.postAsync(httpProgram, new ComyouHttpCallBack() {

			@Override
			public void onResponse(String resString) {
//				progressDialog.dismiss();
				Log.e("tag", resString);
				try {
					if(newListInfos!=null){
						newListInfos.clear();
					}
					JSONObject jsonObject = new JSONObject(resString);
						
						JSONArray dataArray = jsonObject
								.getJSONArray("data");
						if (dataArray != null && dataArray.length() > 0) {
							for (int i = 0; i < dataArray.length(); i++) {

								JSONObject object = (JSONObject) dataArray
										.opt(i);
								int news_id=object.getInt("news_id");
								String title=object.getString("title");
								String add_time=object.getString("add_time");
								String content=object.getString("content");
								String image=object.getString("image");
								int good_num=object.getInt("good_num");
							//	int total_count=object.getInt("total_count");
								int total_count=0;
							//	String news_url=object.getString("news_url");
								String news_url="";
								NewListInfo listInfo=new NewListInfo(news_id, title, add_time, image, content, good_num, total_count,news_url);
								newListInfos.add(listInfo);
								
								LogUtils.e("tag", "image: "+image);
							}
							handler.sendEmptyMessage(1);
						}

				} catch (JSONException e) {
					Log.e("tag", e.toString());
				}

			}

			@Override
			public void onFailure(String arg0, IOException arg1) {
//				progressDialog.dismiss();
//				showToast(getString(R.string.login_fail).toString());
			}

		});
	
	}


}
